<?php
/***
 * 描述：用户登录，注册
 * @category		Mobile		
 * @package			Action			
 * @subpackage		
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: CustPassportAction.class.php 33 2013-10-27 02:28:28Z whtaok@gmail.com $
 */
class CustPassportAction extends MobileAction {
	/**
	 * 描述：初始化模块
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-4-14
	 */
	protected  function _initialize() {
		parent::_initialize();
	}
	/**
	 * 描述：用户登录页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		if(session(SES_CUST_ID)){
			$this->redirect('Cust/index');
		}else{
			$this->assign('lastUrl',I('lastUrl'));
			$this->display('Cust/Passport/login');
		}
	}
	
	/**
	 * 描述：登录操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doLoginAct() {
		$this->_hasLogined();
		$custAccount = $_POST['cust_account'] && trim($_POST['cust_account']) ? trim($_POST['cust_account']) : '';
		$custPass = $_POST['cust_pass'] && trim($_POST['cust_pass']) ? trim($_POST['cust_pass']) : '';
		$cust = null;
		//校验：用户名或密码是否为空
		if($custAccount===''){
			$message = "请输入登录手机号码！";
		}else if($custPass===''){
			$message = "请输入登录密码！";
		}else{
			//校验：用户名或密码是否存在
			$mod = new CustLibModel();
			$modWhere['account'] = $custAccount;
			$cust=$mod->where($modWhere)->find();
			if(!$cust) {
				$message = "帐号或密码不正确！";
			}else if($cust['password'] != md5($custPass)) {
				$message = "帐号或密码不正确！";
			}else if($cust['is_valid'] == 1) {
				$message = "您的账号正在审核中，请联系客服人员！";
			}else if($cust['is_valid'] == 3){
				$message = "您的账号已被禁用，请联系客服人员！";
			}
		}
		
		if($message){
			$rtnData = array(
				'error'=>1,
				'message'=>$message,
			);
		}else{
			$this->mysession_refresh($cust['id']);
			$rtnData = array(
				'error'=>0,
				'message'=>'登录成功',
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：登出操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doLogoutAct() {
		session(SES_CUST_ID,null);
		session(SES_CUST,null);
		$this->redirect('Index/index');
	}
	
	/**
	 * 描述：显示注册页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showRegistAct() {
		$this->display('Cust/Passport/regist');
	}
	
	/**
	 * 描述：注册操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doRegistAct() {
		$this->_hasLogined();
		$data = array(
			'account' => I('cust_account'),
			'password' => md5(I('cust_pass')),
			'password_char' => I('cust_pass'),
			'mobile' => I('cust_account'),
			'name' => I('cust_name'),
			'last_login_time'=>time(),
			'last_login_ip'=>get_client_ip(),
		);
		$mod = new CustLibModel();
		if($mod->create($data)){
			$custId = $mod->add();
			if($custId){
				$this->mysession_refresh($custId);
				$rtnData = array(
						'error'=>0,
						'message'=>'注册成功，祝您购物愉快',
				);
			}else{
				$rtnData = array(
					'error'=>1,
					'message'=>'客户注册失败，请刷新页面重新尝试',
				);
			}
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：显示修改密码页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showChgpwdAct() {
		$this->checkAuth();
		$this->display('Cust/Passport/chgpwd');
	}
	
	/**
	 * 描述：修改密码
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doChgpwdAct() {
		$this->checkAuth();
		//校验：密码和旧密码
		$custPass = I('cust_pass');
		$custPassNew = I('cust_pass_new');
		if(strlen($custPassNew)<6){
			$rtnData = array(
					'error'=>1,
					'message'=>'密码长度至少为6个字符！',
			);
			$this->ajaxReturn($rtnData);
			exit;
		}
		if($custPass==$custPassNew){
			$rtnData = array(
					'error'=>1,
					'message'=>'您输入的新密码和旧密码一致！',
			);
			$this->ajaxReturn($rtnData);
			exit;
		}
		$mod = new CustLibModel();
		$cust=$mod->find(session(SES_CUST_ID));
		if($cust['password']==md5($custPass)){
			$data = array(
				'id'=>session(SES_CUST_ID),
				'password'=>md5($custPassNew),
				'password_char'=>$custPassNew,
			);
			$flag = $mod->save($data);
			if($flag !== false){
				$rtnData = array(
						'error'=>0,
						'message'=>'密码更改成功！',
				);
			}else{
				$rtnData = array(
						'error'=>0,
						'message'=>'密码更改失败，请重新尝试！',
				);
			}
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>'旧密码输入不正确！',
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：显示完善个人信息页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdAct() {
		$this->checkAuth();
		$cust = session(SES_CUST);
		//选择最近的店铺
		$mod = new ShopLibModel();
		$modWhere = array(
				'is_valid'=>'2'
			);
		$shopList = $mod->setModWhere($modWhere)->select();
		$this->assign('cust',$cust);
		$this->assign('shopList',$shopList);
		$this->display('Cust/Passport/person');
	}
	
	/**
	 * 描述：修改个人信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct() {
		$this->checkAuth();
		//校验：输入的信息
		$mod = new CustLibModel();
		if($mod->create()){
			$mod->id=session(SES_CUST_ID);
			$flag = $mod->save();
			if($flag !== false){
				$rtnData = array(
						'error'=>0,
						'message'=>'修改成功！',
				);
				$this->mysession_refresh(session(SES_CUST_ID));
			}else{
				$rtnData = array(
						'error'=>0,
						'message'=>'修改失败，请重新尝试',
				);
			}
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);
	}
	//判断是否已经登录过，如果登录过跳转到用户管理页面
	private function _hasLogined(){
		if(session(SES_CUST_ID)){
			$this->success('您已经登录!',U('Cust/Index'));
		}
	}
}

?>